/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation;

import java.awt.Color;
import java.awt.EventQueue;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import java.util.List;
import java.util.ResourceBundle;
import javax.persistence.RollbackException;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

/**
 *
 * @author kaibiibrahim
 */
public class ClientForm extends JPanel {

    public ClientForm() {
        initComponents();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }
        jLabel1.setVisible(false);
//        jSpinner1.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("gestionPU").createEntityManager();
        query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Client c");
        list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
        masterScrollPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        identiteField = new javax.swing.JTextField();
        emailLabel = new javax.swing.JLabel();
        faxLabel = new javax.swing.JLabel();
        teleFixField = new javax.swing.JTextField();
        teleMobileLabel = new javax.swing.JLabel();
        emailField = new javax.swing.JTextField();
        teleFixLabel = new javax.swing.JLabel();
        faxField = new javax.swing.JTextField();
        cinLabel = new javax.swing.JLabel();
        ancienneteLabel = new javax.swing.JLabel();
        cinField = new javax.swing.JTextField();
        identiteLabel = new javax.swing.JLabel();
        teleMobileField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jSpDate = new javax.swing.JSpinner();
        saveButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        newButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        FormListener formListener = new FormListener();

        setToolTipText("");
        setName("lllll"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${identite}"));
        columnBinding.setColumnName("Identite");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cin}"));
        columnBinding.setColumnName("CIN");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${anciennete}"));
        columnBinding.setColumnName("Ancienneté");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${teleMobile}"));
        columnBinding.setColumnName("Tél. Mobile");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${teleFix}"));
        columnBinding.setColumnName("Tél. Fix");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${email}"));
        columnBinding.setColumnName("Email");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fax}"));
        columnBinding.setColumnName("Fax");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        masterScrollPane.setViewportView(masterTable);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Détailles: "));

        identiteField.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        identiteField.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        identiteField.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.identite}"), identiteField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue("");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), identiteField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        identiteField.addActionListener(formListener);
        identiteField.addFocusListener(formListener);

        emailLabel.setForeground(new java.awt.Color(51, 51, 51));
        emailLabel.setText("Email:");

        faxLabel.setForeground(new java.awt.Color(51, 51, 51));
        faxLabel.setText("Fax:");

        teleFixField.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.teleFix}"), teleFixField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue("");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), teleFixField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        teleFixField.addFocusListener(formListener);

        teleMobileLabel.setForeground(new java.awt.Color(51, 51, 51));
        teleMobileLabel.setText("Tél. Mobile:");

        emailField.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.email}"), emailField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue("");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), emailField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        emailField.addFocusListener(formListener);

        teleFixLabel.setForeground(new java.awt.Color(51, 51, 51));
        teleFixLabel.setText("Tél. Fix:");

        faxField.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.fax}"), faxField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue("");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), faxField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        faxField.addFocusListener(formListener);

        cinLabel.setForeground(new java.awt.Color(51, 51, 51));
        cinLabel.setText("CIN:");

        ancienneteLabel.setForeground(new java.awt.Color(51, 51, 51));
        ancienneteLabel.setText("Ancienneté:");

        cinField.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.cin}"), cinField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue("null");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), cinField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        cinField.addFocusListener(formListener);

        identiteLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        identiteLabel.setForeground(new java.awt.Color(51, 51, 51));
        identiteLabel.setText("Identite:");

        teleMobileField.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.teleMobile}"), teleMobileField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue("");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), teleMobileField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        teleMobileField.addFocusListener(formListener);

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("Veuillez remplir les champs obligatoires");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        jSpDate.setModel(new javax.swing.SpinnerDateModel());
        jSpDate.setEditor(new javax.swing.JSpinner.DateEditor(jSpDate, "dd/MM/yyyy"));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.anciennete}"), jSpDate, org.jdesktop.beansbinding.BeanProperty.create("value"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), jSpDate, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jSpDate.addFocusListener(formListener);
        jSpDate.addPropertyChangeListener(formListener);

        saveButton.setText("Save");
        saveButton.addActionListener(formListener);

        refreshButton.setText("Refresh");
        refreshButton.addActionListener(formListener);

        newButton.setText("New");
        newButton.addActionListener(formListener);

        deleteButton.setText("Delete");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        deleteButton.addActionListener(formListener);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel2.setName("jlblcount"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(identiteLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cinLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(ancienneteLabel, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(identiteField, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jSpDate, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                                .addComponent(cinField, javax.swing.GroupLayout.Alignment.LEADING)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(teleMobileLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(teleFixLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(emailLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(faxLabel, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(emailField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                            .addComponent(teleMobileField, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(teleFixField, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(faxField, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(newButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(refreshButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveButton)))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {deleteButton, newButton, refreshButton, saveButton});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(teleMobileLabel)
                            .addComponent(teleMobileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(teleFixLabel)
                            .addComponent(teleFixField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(identiteField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(identiteLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cinLabel)
                    .addComponent(cinField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(emailLabel)
                    .addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(faxLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(ancienneteLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSpDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(faxField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(saveButton)
                        .addComponent(refreshButton)
                        .addComponent(deleteButton)
                        .addComponent(newButton)))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jLabel2.getAccessibleContext().setAccessibleName("jlblcount");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 729, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10))
        );

        bindingGroup.bind();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener, java.awt.event.FocusListener, java.beans.PropertyChangeListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == deleteButton) {
                ClientForm.this.deleteButtonActionPerformed(evt);
            }
            else if (evt.getSource() == newButton) {
                ClientForm.this.newButtonActionPerformed(evt);
            }
            else if (evt.getSource() == refreshButton) {
                ClientForm.this.refreshButtonActionPerformed(evt);
            }
            else if (evt.getSource() == saveButton) {
                ClientForm.this.saveButtonActionPerformed(evt);
            }
            else if (evt.getSource() == identiteField) {
                ClientForm.this.identiteFieldActionPerformed(evt);
            }
        }

        public void focusGained(java.awt.event.FocusEvent evt) {
            if (evt.getSource() == jSpDate) {
                ClientForm.this.jSpDateFocusGained(evt);
            }
            else if (evt.getSource() == teleMobileField) {
                ClientForm.this.teleMobileFieldFocusGained(evt);
            }
            else if (evt.getSource() == cinField) {
                ClientForm.this.cinFieldFocusGained(evt);
            }
            else if (evt.getSource() == faxField) {
                ClientForm.this.faxFieldFocusGained(evt);
            }
            else if (evt.getSource() == emailField) {
                ClientForm.this.emailFieldFocusGained(evt);
            }
            else if (evt.getSource() == teleFixField) {
                ClientForm.this.teleFixFieldFocusGained(evt);
            }
            else if (evt.getSource() == identiteField) {
                ClientForm.this.identiteFieldFocusGained(evt);
            }
        }

        public void focusLost(java.awt.event.FocusEvent evt) {
            if (evt.getSource() == jSpDate) {
                ClientForm.this.jSpDateFocusLost(evt);
            }
            else if (evt.getSource() == teleMobileField) {
                ClientForm.this.teleMobileFieldFocusLost(evt);
            }
            else if (evt.getSource() == cinField) {
                ClientForm.this.cinFieldFocusLost(evt);
            }
            else if (evt.getSource() == faxField) {
                ClientForm.this.faxFieldFocusLost(evt);
            }
            else if (evt.getSource() == emailField) {
                ClientForm.this.emailFieldFocusLost(evt);
            }
            else if (evt.getSource() == teleFixField) {
                ClientForm.this.teleFixFieldFocusLost(evt);
            }
            else if (evt.getSource() == identiteField) {
                ClientForm.this.identiteFieldFocusLost(evt);
            }
        }

        public void propertyChange(java.beans.PropertyChangeEvent evt) {
            if (evt.getSource() == jSpDate) {
                ClientForm.this.jSpDatePropertyChange(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        //         if (!this.validForm()) {
            //            return;
            //        }
        int[] selected = masterTable.getSelectedRows();
        List<entity.Client> toRemove = new ArrayList<entity.Client>(selected.length);
        for (int idx = 0; idx < selected.length; idx++) {
            entity.Client c = list.get(masterTable.convertRowIndexToModel(selected[idx]));
            toRemove.add(c);
            entityManager.remove(c);
        }
        list.removeAll(toRemove);
        nbrOpertation += selected.length;
        //jSpinner1.setEnabled(false);
        this.validForm();
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed

        if (!valid && !this.validForm()) {
            return;
        }
        entity.Client c = new entity.Client();
        c.setAnciennete(new Date());
        entityManager.persist(c);
        list.add(c);
        int row = list.size() - 1;
        masterTable.setRowSelectionInterval(row, row);
        masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
        this.emptyFields();
        //jSpinner1.setEnabled(true);
        nbrOpertation += 1;
        valid = this.validForm();
    }//GEN-LAST:event_newButtonActionPerformed

    @SuppressWarnings("unchecked")
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        //         if (!this.validForm()) {
            //            return;
            //        }
        entityManager.getTransaction().rollback();
        entityManager.getTransaction().begin();
        java.util.Collection data = query.getResultList();
        for (Object entity : data) {
            entityManager.refresh(entity);
        }
        list.clear();
        list.addAll(data);
        //jSpinner1.setEnabled(false);
        nbrOpertation = 0;
        this.validForm();
    }//GEN-LAST:event_refreshButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        if (!validForm()) {
            return;
        }
        try {
            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();

            if (nbrOpertation > 0) {
                JOptionPane.showMessageDialog(this, "L'opération est terminé avec succée", "Information", JOptionPane.INFORMATION_MESSAGE);
            }

        } catch (RollbackException rex) {
            rex.printStackTrace();
            entityManager.getTransaction().begin();
            List<entity.Client> merged = new ArrayList<entity.Client>(list.size());
            for (entity.Client c : list) {
                merged.add(entityManager.merge(c));
            }
            list.clear();
            list.addAll(merged);
        }
        nbrOpertation = 0;
        mustSave(nbrOpertation);
    }//GEN-LAST:event_saveButtonActionPerformed

    private void jSpDatePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jSpDatePropertyChange
        // TODO add your handling code here:
        if (evt.getPropertyName().equals("enabled") && evt.getNewValue().toString().equals("false")) {
            Calendar calendar = new GregorianCalendar();
            jSpDate.setValue(calendar.getTime());
        }
    }//GEN-LAST:event_jSpDatePropertyChange

    private void jSpDateFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jSpDateFocusLost
        // TODO add your handling code here:
        if (!ancienneteValue.equals(jSpDate.getValue().toString())) {
            nbrOpertation += 1;
        }
    }//GEN-LAST:event_jSpDateFocusLost

    private void jSpDateFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jSpDateFocusGained
        // TODO add your handling code here:
        ancienneteValue = jSpDate.getValue().toString();
    }//GEN-LAST:event_jSpDateFocusGained

    private void teleMobileFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_teleMobileFieldFocusLost
        // TODO add your handling code here:
        if (!teleMobileValue.equals(teleMobileField.getText())) {
            nbrOpertation += 1;
        }
    }//GEN-LAST:event_teleMobileFieldFocusLost

    private void teleMobileFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_teleMobileFieldFocusGained
        // TODO add your handling code here:
        teleMobileValue = teleMobileField.getText();
    }//GEN-LAST:event_teleMobileFieldFocusGained

    private void cinFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cinFieldFocusLost
        // TODO add your handling code here:
        if (!cinValue.equals(cinField.getText())) {
            nbrOpertation += 1;
        }
    }//GEN-LAST:event_cinFieldFocusLost

    private void cinFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cinFieldFocusGained
        // TODO add your handling code here:
        cinValue = cinField.getText();
    }//GEN-LAST:event_cinFieldFocusGained

    private void faxFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_faxFieldFocusLost
        // TODO add your handling code here:
        if (!faxValue.equals(faxField.getText())) {
            nbrOpertation += 1;
        }
    }//GEN-LAST:event_faxFieldFocusLost

    private void faxFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_faxFieldFocusGained
        // TODO add your handling code here:
        faxValue = faxField.getText();
    }//GEN-LAST:event_faxFieldFocusGained

    private void emailFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_emailFieldFocusLost
        // TODO add your handling code here:
        if (!emailValue.equals(emailField.getText())) {
            nbrOpertation += 1;
        }
    }//GEN-LAST:event_emailFieldFocusLost

    private void emailFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_emailFieldFocusGained
        // TODO add your handling code here:
        emailValue = emailField.getText();
    }//GEN-LAST:event_emailFieldFocusGained

    private void teleFixFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_teleFixFieldFocusLost
        // TODO add your handling code here:
        if (!teleFixValue.equals(teleFixField.getText())) {
            nbrOpertation += 1;
        }
    }//GEN-LAST:event_teleFixFieldFocusLost

    private void teleFixFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_teleFixFieldFocusGained
        // TODO add your handling code here:
        teleFixValue = teleFixField.getText();
    }//GEN-LAST:event_teleFixFieldFocusGained

    private void identiteFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_identiteFieldFocusLost
        // TODO add your handling code here:
        if (!identiteValue.equals(identiteField.getText())) {
            nbrOpertation += 1;
        }
    }//GEN-LAST:event_identiteFieldFocusLost

    private void identiteFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_identiteFieldFocusGained
        // TODO add your handling code here:
        identiteValue = identiteField.getText();
    }//GEN-LAST:event_identiteFieldFocusGained

    private void identiteFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_identiteFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_identiteFieldActionPerformed

    void mustSave(int nbr) {
        if (nbr > 0) {
            this.setToolTipText("*");
        } else {
            this.setToolTipText("*");
        }

    }

    boolean validForm() {
        boolean temp = true;
        if (identiteField.getText().equalsIgnoreCase("") && identiteField.isEnabled()) {
            identiteField.setBorder(BorderFactory.createLineBorder(Color.RED));
            identiteLabel.setForeground(Color.RED);
            masterTable.setEnabled(false);
//                identiteField.setBackground(Color.RED);
//                identiteField.setForeground(Color.WHITE);
            temp = false;
        } else {
            identiteField.setBorder(BorderFactory.createEtchedBorder());
            identiteLabel.setForeground(Color.BLACK);
            masterTable.setEnabled(true);
//                identiteField.setBackground(Color.WHITE);
//                identiteField.setForeground(Color.BLACK);
        }
        jLabel1.setVisible(!temp);
        return temp;
    }

    void emptyFields() {
        identiteField.setText("");
        cinField.setText("");
        teleFixField.setText("");
        teleMobileField.setText("");
        emailField.setText("");
        faxField.setText("");
        jSpDate.setValue(new Date());
    }
    int nbrOpertation = 0;
    boolean valid = true;    String identiteValue;    String cinValue;    String ancienneteValue;
    String teleMobileValue;    String teleFixValue;
    String emailValue;    String faxValue;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ancienneteLabel;
    private javax.swing.JTextField cinField;
    private javax.swing.JLabel cinLabel;
    private javax.swing.JButton deleteButton;
    private javax.swing.JTextField emailField;
    private javax.swing.JLabel emailLabel;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JTextField faxField;
    private javax.swing.JLabel faxLabel;
    private javax.swing.JTextField identiteField;
    private javax.swing.JLabel identiteLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSpinner jSpDate;
    private java.util.List<entity.Client> list;
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private javax.swing.JButton newButton;
    private javax.persistence.Query query;
    private javax.swing.JButton refreshButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextField teleFixField;
    private javax.swing.JLabel teleFixLabel;
    private javax.swing.JTextField teleMobileField;
    private javax.swing.JLabel teleMobileLabel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    public static void main(String[] args) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ClientForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ClientForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ClientForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClientForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setContentPane(new ClientForm());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);

            }
        });
    }

}
